package cn.ffcs.uec.organization.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.organization.component.bean.UecOrganizationOrgsListboxExtBean;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年5月27日
 */
@Controller
@Scope("prototype")
public class UecOrganizationOrgsListboxExt extends Div implements IdSpace {

    /**
     * 
     */
    private static final long serialVersionUID = 4414802770164400134L;

    @Getter
    private UecOrganizationOrgsListboxExtBean bean = new UecOrganizationOrgsListboxExtBean();

    /**
     * zul.
     */
    private static final String zul = "/pages_nmg/uec_organization/comp/uecOrganizationOrgs_listbox_ext.zul";

    private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
            .getBean("uecOrgOrgsManager");

    private UecOrgOrgs uecOrgOrgs;

    private UecOrgOrgs eventUecOrgOrgs;

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecOrganizationOrgsListboxExt() {
        // 1.绑定页面
        Executions.createComponents(zul, this, null);
        // 2.页面控件与java属性绑定
        Components.wireVariables(this, bean);
        // 3.绑定监听事件
        Components.addForwards(this, this, '$');
        // 转发
        this.addForward("onOrganizationOrgsQuery", this, "onOrganizationOrgsQueryResponse");
    }

    /**
     * 初始化
     */
    public void onCreate() {
        onQuery();
    }

    /**
     * 接受抛出的查询事件
     * 
     * @param event
     */
    public void onOrganizationOrgsQueryResponse(final ForwardEvent event) {
        eventUecOrgOrgs = (UecOrgOrgs) event.getOrigin().getData();
        bean.getCode().setValue(null);
        bean.getName().setValue(null);
        onQuery();
    }

    /**
     * 双击触发选中事件
     */
    public void onSelectRequest() throws Exception {
        if (bean.getUecOrgOrgsListbox().getSelectedCount() > 0) {
            uecOrgOrgs = (UecOrgOrgs) bean.getUecOrgOrgsListbox().getSelectedItem().getValue();
            // 集团负责人数剧不为空，省内负责人不允许修改
            if (!StrUtil.isNullOrEmpty(uecOrgOrgs.getPrincipal()) && !"~".equals(uecOrgOrgs.getPrincipal())) {
                ZkUtil.showError("集团下发的负责人不为空，省内负责人不允许修改！", "温馨提示");
                return;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "edit");
            map.put("uecOrgOrgs", uecOrgOrgs);
            Window window = (Window) Executions
                    .createComponents("/pages_nmg/uec_organization/comp/uec_organization_info_main.zul", this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        uecOrgOrgs = (UecOrgOrgs) event.getData();
                        queryOrganization();
                    }
                }
            });
        }
    }

    /**
     * 查询
     */
    public void onQuery() {
        uecOrgOrgs = new UecOrgOrgs();
       /* String orgName = PermissionUtil.getManagerOrg();
        if ("区本部".equals(orgName)) {
            uecOrgOrgs.setPkFatherorg("0001A11000000003UHTY");
        }*/
        if (!StrUtil.isNullOrEmpty(eventUecOrgOrgs)) {
            uecOrgOrgs.setPkFatherorg(eventUecOrgOrgs.getPkOrg());
        }
        uecOrgOrgs.setCode(bean.getCode().getValue());
        uecOrgOrgs.setName(bean.getName().getValue());
        this.queryOrganization();
    }

    /**
     * 查询
     */
    public void queryOrganization() {
        int activePage = bean.getUecOrgOrgsListboxPaging().getActivePage();
        int pageSize = bean.getUecOrgOrgsListboxPaging().getPageSize();
        PageInfo pageInfo = uecOrgOrgsManager.queryOrganization(uecOrgOrgs, activePage + 1, pageSize);
        ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
        // 总条数
        bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
        bean.getUecOrgOrgsListbox().setModel(dataList);
        bean.getUecOrgOrgsListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
    }

    /**
     * 重置
     */
    public void onReset() {
        bean.getName().setValue(null);
        bean.getCode().setValue(null);
        bean.getTotalSize().setValue(null);
        clearVariable();
        ListboxUtils.clearListbox(this.bean.getUecOrgOrgsListbox());
        this.bean.getUecOrgOrgsListboxPaging().setTotalSize(1);
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecOrgOrgs = null;
    }

    /**
     * 翻页
     */
    public void onListboxPaging() {
        this.queryOrganization();
    }
}
